﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.IO;
using DocumentFormat.OpenXml.Packaging;

namespace docExplorer
{
    public partial class uc_Search : UserControl
    {
        public uc_Search()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text != string.Empty && rbtDocName.Checked)
            {
                this.tvwSearch.Nodes.Clear();
                this.SearchByFileName(txtSearch.Text);
            }
            else if (txtSearch.Text != string.Empty && rbtDocContents.Checked)
            {
                this.tvwSearch.Nodes.Clear();
                this.SearchByDocContents(txtSearch.Text);
            }
        }

        private void SearchByFileName(string fileName)
        {
            uc_Explorer Explorer = new uc_Explorer();
            TreeNode[] searchNodes = Explorer.RetrieveTreeNodes();
            foreach (TreeNode node in searchNodes)
            {
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Category)
                {
                    this.SearchByFileName(fileName, node.Nodes);
                }
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Doc &&
                    node.Text.Contains(fileName))
                {
                    tvwSearch.Nodes.Add(node);
                }
            }
        }

        private void SearchByFileName(string fileName, TreeNodeCollection searchNodes)
        {
            foreach (TreeNode node in searchNodes)
            {
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Category)
                {
                    this.SearchByFileName(fileName, node.Nodes);
                }
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Doc &&
                    node.Text.Contains(fileName))
                {
                    tvwSearch.Nodes.Add(node);
                }
            }
        }

        private void SearchByDocContents(string content)
        {
            uc_Explorer Explorer = new uc_Explorer();
            TreeNode[] searchNodes = Explorer.RetrieveTreeNodes();
            foreach (TreeNode node in searchNodes)
            {
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Category)
                {
                    this.SearchByDocContents(content, node.Nodes);
                }
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Doc)
                {
                    using (WordprocessingDocument wdoc = WordprocessingDocument.Open(((TreeNodeContents)node.Tag).Path, false))
                    {
                        XDocument xDoc = XDocument.Load(XmlReader.Create(wdoc.MainDocumentPart.GetStream()));
                        if (xDoc.ToString().Contains(content))
                        {
                            tvwSearch.Nodes.Add(node);
                        }
                    }
                }
            }
        }

        private void SearchByDocContents(string content, TreeNodeCollection searchNodes)
        {
            foreach (TreeNode node in searchNodes)
            {
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Category)
                {
                    this.SearchByDocContents(content, node.Nodes);
                }
                if (((TreeNodeContents)node.Tag).Type == TreeNodeType.Doc)
                {
                    using (WordprocessingDocument wdoc = WordprocessingDocument.Open(((TreeNodeContents)node.Tag).Path, false))
                    {
                        XDocument xDoc = XDocument.Load(XmlReader.Create(wdoc.MainDocumentPart.GetStream()));
                        if (xDoc.ToString().Contains(content))
                        {
                            tvwSearch.Nodes.Add(node);
                        }
                    }
                }
            }
        }
    }
}
